//
//  NetworkManager+extenstion.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/12.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit

extension NetworkManager
{
    /**
     获取令牌
     
     - parameter code:     登录成功后返回的code
     - parameter callBack: 回调闭包
     */
    func requestAccessToken(code: String, callBack:([String : AnyObject]?) ->Void)
    {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let requestType = RequestType.POST
        let parameters = ["client_id":appKey, "client_secret": appSecrect, "grant_type": "authorization_code", "redirect_uri": redirectURL, "code": code]
        self.request(urlString, type: requestType, parameters: parameters) { (response) -> Void in
            callBack(response)
        }
        
    }
    /**
     获取用户信息
     
     - parameter accessTokec: 令牌
     - parameter uid:         uid
     - parameter callBack:    回调闭包
     */
    func requestUserAccount(accessTokec : String,uid: String, callBack: ([String :AnyObject]?) ->Void)
    {
        let urlString = "https://api.weibo.com/2/users/show.json"
        let requsetType = RequestType.GET
        let parameters = ["access_token" : accessTokec,"uid" :uid ]
        request(urlString, type: requsetType, parameters: parameters) { (response) -> Void in
            callBack(response)
        }
    }
    
    func revokeOAuth()
    {
        if let accessToken = UserAccount.shareUserAccount.access_token {
            let urlString = "https://api.weibo.com/oauth2/revokeoauth2?access_token=\(accessToken)"
            self.request(urlString, type: .GET, parameters: nil, callBack: { (_) in
                //1.把本地UserDic删除掉
                NSUserDefaults.standardUserDefaults().setObject(nil, forKey: userAccountKey)
                UserAccount.shareUserAccount.access_token = nil
            })
        }
    }

}
